package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.apis.article.IArticleClient;
import com.heima.model.admin.admin.ChannelDto;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.wemedia.dtos.WmChannelDto;
import com.heima.model.wemedia.pojos.WmChannel;
import com.heima.wemedia.mapper.WmChannelMapper;
import com.heima.wemedia.service.WmChannelService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 频道信息表 服务实现类
 * </p>
 *
 * @author itheima
 * @since 2022-03-16
 */
@Service
@Transactional
@Slf4j
public class WmChannelServiceImpl extends ServiceImpl<WmChannelMapper, WmChannel> implements WmChannelService {
    @Autowired
    private WmChannelMapper wmChannelMapper;
    @Autowired
    private IArticleClient iArticleClient;

    /**
     * 查询所有的频道
     *
     * @return
     */
    @Override
    public ResponseResult getChannels() {
        return ResponseResult.okResult(list());
    }

    /**
     * 添加频道
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult saveChannel(WmChannelDto wmChannel) {
        //1.检查参数
        //参数转换
        WmChannel wmChannel1 = new WmChannel();
        try {
            BeanUtils.copyProperties(wmChannel1, wmChannel);
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (wmChannel == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
//        if (wmChannel.getStatus()==1) {
        if (wmChannel.getStatus()) {
            wmChannel1.setStatus(true);
        } else {
            wmChannel1.setStatus(false);
        }
        //2.新增频道
        wmChannel.setCreatedTime(new Date());
        save(wmChannel1);
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 分页查询频道
     *
     * @param dto
     * @return
     */
    @Override
    public ResponseResult sybaseChannels(ChannelDto dto) {
        Page<WmChannel> page = new Page<>(dto.getPage(), dto.getSize());
        LambdaQueryWrapper<WmChannel> queryWrapper = new LambdaQueryWrapper<WmChannel>();
        if (dto.getName() != null) {
            queryWrapper.like(WmChannel::getName, dto.getName());
        }
//        Page<WmChannel> wmChannelPage = wmChannelMapper.selectPage(page, queryWrapper);
        page = wmChannelMapper.selectPage(page, queryWrapper);
        ResponseResult responseResult = new PageResponseResult(dto.getPage(), dto.getSize(), (int) page.getTotal());
        responseResult.setData(page.getRecords());
        return responseResult;
    }

    /**
     * 修改频道
     *
     * @param wmChannel
     * @return
     */
    @Override
    public ResponseResult updateChannel(WmChannelDto wmChannel) {
        //参数校验
        log.info("修改方法接收到的参数:" + wmChannel);
        if (wmChannel.getId() < 0) {
            return ResponseResult.errorResult(501, "参数有误");
        }
        //是否可以操作
        ResponseResult responseResult = iArticleClient.lookChannelName(wmChannel);

        //System.out.println(responseResult.getData() + "@------------------------");
        //执行逻辑
        if ((boolean) responseResult.getData()) {
            return ResponseResult.errorResult(501, "频道有引用无法修改");
        }
        //参数处理
        WmChannel wmChannel1 = new WmChannel();
        try {
            BeanUtils.copyProperties(wmChannel1, wmChannel);
        } catch (Exception e) {
            e.printStackTrace();
        }
//        if (wmChannel.getStatus() == 1) {
        if (wmChannel.getStatus()) {
            wmChannel1.setStatus(true);
        } else {
            wmChannel1.setStatus(false);
        }


        wmChannelMapper.updateById(wmChannel1);
        //添加返回值
        return ResponseResult.okResult("ok");
    }

    /**
     * 删除频道
     *
     * @param id
     * @return
     */
    @Override
    public ResponseResult deleChannel(Integer id) {
        //1.检查参数
        if (id == null) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        WmChannel wmChannel = wmChannelMapper.selectById(id);
        if (wmChannel.getStatus()) {
            return ResponseResult.errorResult(AppHttpCodeEnum.CHANNEL_ACTIVE);
        }
        wmChannelMapper.deleteById(id);
        return ResponseResult.okResult("ok");
    }
}

